"""
用户视图层
"""
import threading
import time

from interface import init_interface, login_interface, connect_interface, auth_interface, sync_interface, \
    initialization_interface
from lib.common import host_row_search

host_list = []
browser, wait = (None, None)
init_sum = 0


def web_init():
    """
    初始化
    """
    global host_list, browser, wait
    init_interface.init_settings()
    host_list = init_interface.host_init_interface()
    browser, wait = init_interface.browser_init_interface()


def login():
    """
    4A登录
    """
    login_interface.login_interface(browser, wait)


def link_test(host_row, host):
    """
    系统版本获取
    测试连接
    :return: 
    """
    print(f"------------------第{host.host_id}台主机---------------------")
    connect_interface.link_test_interface(browser, wait, host_row, host)
    return host_row


def host_initialization(host_row, host):
    """
    主机初始化
    """
    initialization_interface.initializing_interface(browser, wait, host_row, host)


def manual_sync(host_row, host):
    """
    查询从账号
    手工同步
    :return:
    """
    is_exist = sync_interface.account_query_interface(browser, wait, host_row, host)
    # if is_exist is True: 都做手工同步
    if is_exist is None or is_exist is True:
        return
    sync_interface.manual_sync_interface(browser, wait, host)


i = 0


def run():
    web_init()
    login()
    while True:
        global i
        if i >= len(host_list):
            return
        host = host_list[i]
        # host_row = host_row_search(browser, host)
        # if host_row is None:
        #     continue
        # host_row = link_test(host_row, host)
        # # host_initialization(host_row, host)
        # manual_sync(host_row, host)
        sync_interface.only_manual_sync_interface(browser, wait, host)
        # time.sleep(3)
        i += 1


if __name__ == '__main__':
    run()
